package com.cloud.bssp.netty.shortconnection;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @description： TODO
 * @author：weirx
 * @date：2021/11/12 15:34
 * @version：3.0
 */
public class HalfPackageClient {
    public static void main(String[] args) {
        //循环发送，每次18字节，共10次
        for (int i = 0; i < 10; i++) {
            NioEventLoopGroup worker = new NioEventLoopGroup();
            try {
                Bootstrap bootstrap = new Bootstrap();
                bootstrap.channel(NioSocketChannel.class);
                bootstrap.group(worker);
                bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                            //建立连接成功后，会触发active事件
                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {

                                ByteBuf buffer = ctx.alloc().buffer();
                                buffer.writeBytes(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,16,17});
                                ctx.writeAndFlush(buffer);
                                ctx.channel().close();
                            }
                        });
                    }
                });
                ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8080).sync();
                //释放连接
                channelFuture.channel().closeFuture().sync();

            } catch (InterruptedException e) {
                System.out.println("client error :" + e);
            } finally {
                //释放EventLoopGroup
                worker.shutdownGracefully();
            }
        }
    }
}
